package com.azh.springcloud.controller;


import com.azh.springcloud.entities.CommonResult;
import com.azh.springcloud.entities.Payment;
import com.azh.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.sleuth.zipkin2.ZipkinProperties;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@Slf4j
public class PaymentController {
    @Resource
    private PaymentService paymentService;
    @Resource
    private DiscoveryClient discoveryClient;
    @Value("${server.port}")
    private  String serverPort;
    @PostMapping(value = "/payment/create")
    //浏览器不支持post，所以需要用idea的http工具
    //RequestBody，自动匹配值，可以同步到数据库
    public CommonResult create(@RequestBody Payment payment){
        int result = paymentService.create(payment);
        // log.info("结果为"+result+"hohohoskdnskjdnsd");
        if(result>0){
            return new CommonResult(200,"插入成功,serverPort:"+serverPort,result);
        }else{
            return new CommonResult(444,"插入失败",null);
        }
    }
    @GetMapping(value = "/payment/get/{id}")
    public CommonResult<Payment> getPaymentByid(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentByid(id);
        log.info("结果为"+payment);
        if(payment!=null){
            return new CommonResult(200,"查询成功,serverPort:"+serverPort,payment);
        }else{
            return new CommonResult(444,"查询失败,ID为:"+id,null);
        }
    }
    @GetMapping("/payment/discovery")
    public Object discovery(){
        List<String> services = discoveryClient.getServices();
        return this.discoveryClient;
    }

}
